<template>
  <div class="container">
    <el-dialog title="新增学科" :visible.sync="dialogShow" width="25%">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          {{ form.isFrontDisplay }}
          <span>不可见</span>
          <el-switch
            v-model="form.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
          <span>可见</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入新增学科接口
import { add } from '@/api/hmmm/subjects.js'
export default {
  // 控制面板的显示与隐藏
  props: ['dialogShow'],
  data() {
    return {
      // 表单数据源
      form: {
        // 学科名称
        subjectName: '',
        isFrontDisplay: ''
      },
      // 表单校验规则
      rules: {
        subjectName: [{ required: true, message: '请输入学科名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    getData(type) {
      this.form.type = type
    },
    // 关闭面板.
    cancle() {
      this.$emit('update:dialogShow', false)
      // 清除数据
      this.form = {
        subjectName: '',
        isFrontDisplay: ''
      }
    },
    // 提交数据
    submit() {
      // 全局验证
      this.$refs.form.validate(async res => {
        if (res) {
          // 提交数据到服务器
          await add(this.form)
          // 关闭面板
          this.$emit('update:dialogShow', false)
          // 提示添加成功
          this.$message.success('添加成功')
          // 清空数据
          this.form = {
            subjectName: '',
            isFrontDisplay: ''
          }
          // 更新数据
          this.$emit('updateData')
        } else {
          console.log('验证不通过')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss"></style>
